<?php

class TaskDao extends Dao {
	
    function __construct() {
        parent::__construct();
        $this->table = 'task';
    }

    public function insertObject($obj){
        
            $result = DB::insert(DB::raw("INSERT INTO task (name, start_date, due_date, priority_id, status_id, project_id, notes, created_by, created_date) ".
                    "VALUES ( '".$obj->getName()."' ".
                            ", '".$obj->getStartDate()."' ".
                            ", '".$obj->getDueDate()."' ".
                            ", '".$obj->getPriority()->getId()."' ".
                            ", '".$obj->getStatus()->getId()."' ".
                            ", '".$obj->getProject()->getId()."' ".
                            ", '".$obj->getNotes()."' ".
                            ", '".$obj->getCreatedBy()->getId()."' ".
                            ", NULL ".
                            ")"));
            if (!$result) { return false; }

            return DB::getPdo()->lastInsertId();
    }

    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE task SET name='".$obj->getName()."'"
                . ", start_date='".$obj->getStartDate()."'"
                . ", due_date='".$obj->getDueDate()."'"
                . ",priority_id='".$obj->getPriority()->getId()."'"
                . ", status_id='".$obj->getStatus()->getId()."'"
                . ", project_id='".$obj->getProject()->getId()."'"
                . ", notes='".$obj->getNotes()."'"
                . ", last_updated_by='".$obj->getLastUpdatedBy()->getId()."'"
                . ", last_updated_date=null"
                . " WHERE id=".$old_id." "));
    return true;
    }
    
    
    public function updateStatus($status, $id){
        DB::update(DB::raw("UPDATE task SET status_id='".$status."' WHERE id=".$id." "));
    return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM task where id='.$id.'"));
        
        return true;
    
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM task where id='".$id."'"));

        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM task"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    public function getListByProjectId($projectId){
        $result = DB::select(DB::raw("SELECT * FROM task where project_id='$projectId'"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
        return $list;
    }
    
    protected function toObject($rowdata=null){
        $obj = new Task();
        $obj->setId($rowdata->id);
        $obj->setName($rowdata->name);
        $obj->setStartDate($rowdata->start_date);
        $obj->setDueDate($rowdata->due_date);
            
            $priority = new Priority();
            $priority->setId($rowdata->priority_id);
            $priority->setIsLoaded(false);
            $obj->setPriority($priority);
            
            $status = new Status();
            $status->setId($rowdata->status_id);
            $status->setIsLoaded(false);
            $obj->setStatus($status);
        
            $project = new Project();
            $project->setId($rowdata->project_id);
            $project->setIsLoaded(false);
            $obj->setProject($project);
        
        $obj->setNotes($rowdata->notes);
        
            $createdBy = new User();
            $createdBy->setId($rowdata->created_by);
            $createdBy->setIsLoaded(false);
            $obj->setCreatedBy($createdBy);
            
            $updateBy = new User();
            $updateBy->setId($rowdata->last_updated_by);
            $updateBy->setIsLoaded(false);
            $obj->setLastUpdatedDate($updateBy);
        $obj->setCreatedDate($rowdata->created_date);
        $obj->setLastUpdatedDate($rowdata->last_updated_date);
        
        return $obj;
    }
	
    
}

   